<template>
  <div class="login_body">
    <div>
      <input
        class="login_text"
        type="text"
        placeHolder="账户名/手机号/Email"
        v-model="un"
      />
    </div>
    <div>
      <input
        class="login_text"
        type="password"
        placeHolder="请输入您的密码"
        v-model="pw"
      />
    </div>
    <div class="login_btn">
      <el-button class="loginBtn" @click="loginAccount">登录</el-button>
    </div>

    <div class="login_link">
      <router-link to="/mine/reg" tag="a">立即注册</router-link>
      <a href="">找回密码</a>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      un: "",
      pw: "",
    };
  },
  methods: {
    async loginAccount() {
      let data = await this.$axios.post("/users/login", {
        username: this.un,
        password: this.pw,
      });
      console.log(data);
      if (data.code === 0) {
        // 修改 vuex 里面的数据
        let obj = {
          isLogin: true,
        };
        this.$store.commit("login/LOGIN_FLAG", obj);
        console.log(this.$store.state.login.isLogin);
        window.localStorage.setItem(
          "user",
          JSON.stringify(this.$store.state.login)
        );
        this.$router.push("/mine/user");
      }
      return false;
    },
  },
  // 组件内的守卫
  beforeRouteEnter(to, from, next) {
    let path = to.path;
    let flag = JSON.parse(window.localStorage.getItem("user")) || "";
    if (path == "/mine/login" && flag) {
      next("/mine/user");
    } else {
      next();
    }
  },
};
</script>

<style scoped>
#content .login_body {
  width: 100%;
}
.login_body .login_text {
  width: 100%;
  height: 40px;
  border: none;
  border-bottom: 1px #ccc solid;
  margin-bottom: 5px;
  outline: none;
  text-indent: 10px;
}
.login_body .login_btn {
  height: 50px;
  margin: 10px;
}
.login_body .login_btn .loginBtn {
  width: 100%;
  height: 100%;
  background: #e54847;
  border-radius: 3px;
  border: none;
  display: block;
  color: white;
  font-size: 20px;
}
.login_body .login_link {
  display: flex;
  justify-content: space-between;
}
.login_body .login_link a {
  text-decoration: none;
  margin: 0 5px;
  font-size: 12px;
  color: #e54847;
}
</style>